package com.michaelbulava.TFS.vcs.internal;

import com.intellij.openapi.vcs.FilePath;
import com.intellij.openapi.vcs.RepositoryLocation;
import com.intellij.openapi.vcs.VcsException;
import com.michaelbulava.TFS.Messages;
import com.microsoft.tfs.core.clients.versioncontrol.soapextensions.Workspace;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by MichaelA on 8/18/2015.
 */
public class TFSRepositoryLocation implements RepositoryLocation {
    private final Map<Workspace, List<FilePath>> myPathsByWorkspaces;

    public TFSRepositoryLocation(Map<Workspace, List<FilePath>> pathsByWorkspaces) {
        this.myPathsByWorkspaces = pathsByWorkspaces;
    }

    @Override
    public String toPresentableString() {
        if (myPathsByWorkspaces.size() == 1 && myPathsByWorkspaces.values().size() == 1)
            return myPathsByWorkspaces.values().iterator().next().get(0).getPresentableUrl();
        else
            return Messages.getString("TFSRepositoryLocation.PresentableString_Multiple");

    }

    @Override
    public String getKey() {
        return toString();
    }

    @Override
    public void onBeforeBatch() throws VcsException {}

    @Override
    public void onAfterBatch() {}

    @Override
    public String toString(){
        StringBuilder sb = new StringBuilder();
        for(Iterator<List<FilePath>> listItem = myPathsByWorkspaces.values().iterator();
                listItem.hasNext();){
            List<FilePath> paths = listItem.next();
            Iterator<FilePath> filePathIterator = paths.iterator();
            while (filePathIterator.hasNext()){
                FilePath path = filePathIterator.next();
                sb.append(path.getPath());
            }
        }
        return sb.toString();
    }

    public Map<Workspace, List<FilePath>> getPathsByWorkspaces(){
        return myPathsByWorkspaces;
    }
}
